﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Model;
using MsDAL;
using System.ComponentModel;
using Components;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public partial class Sys7a00Manager : Sys7a00Base
    {
        private static Sys7a00Manager _entity;

        public static Sys7a00Manager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new Sys7a00Manager();
                return _entity;
            }
        }

        #region 个人代码
        public string SaveData(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            List<SqlApp> lst = new List<SqlApp>();

            string comcode = Validation.ToString(tb["comcode"]);
            string pagename = Validation.ToString(tb["pagename"]);
            Hashtable hsWhere = new Hashtable();
            hsWhere["@a"] = pagename;
            hsWhere["@b"] = comcode;
            hsWhere["@c"] = usercode;

            //Sys7a00表主键
            string sys7a00Listid = Validation.ToString(CommonManager.Entity.ExecDbFunScalar("getsys7a00listid", hsWhere));

            Sys7a00 model = null;
            Sys7a01 modelDtl = null;
            bool isNew = string.IsNullOrEmpty(sys7a00Listid);

            if ("W" == action) //保存当前的排序信息
            {
                if (isNew)
                {
                    //保存概要表身
                    model = new Sys7a00();

                    model.Comcode = comcode;
                    model.Listid = Sys0a60Manager.Entity.CreateSn("sys0a70_listid", usercode);
                    model.Usercode = usercode;
                    model.Pagename = pagename;
                }
                else
                {
                    //先删除明细表
                    lst.Add(sys7a00Dal.DelDetail(sys7a00Listid));
                }

                sys7a00Listid = isNew ? model.Listid : sys7a00Listid;
                if (null != dtl)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (DataRow dr in dtl.Rows) //增加明细表的数据
                    {
                        modelDtl = new Sys7a01();
                        modelDtl.Colname = Validation.ToString(dr["colname"]);
                        modelDtl.Orderid = Validation.ToInt(dr["orderid"]);
                        modelDtl.Plistid = sys7a00Listid;
                        modelDtl.Sortype = Validation.ToString(dr["sorttype"]);

                        lst.Add(Sys7a01Manager.Entity.Add(modelDtl));

                        if (sb.Length != 0)
                        {
                            sb.Append(",");
                        }

                        sb.Append(string.Format("{0} {1}", modelDtl.Colname, modelDtl.Sortype == "A" ? "ASC" : "DESC"));
                    }

                    if (isNew)
                    {
                        model.Sortstr = sb.ToString();
                        lst.Insert(0, this.Add(model)); //回写概要表
                    }
                    else //修改
                    {
                        lst.Add(sys7a00Dal.UpdateSortStr(sys7a00Listid, sb.ToString()));
                    }
                }
            }
            else if ("R" == action)
            {
                if (!isNew)
                {
                    //先删除明细表
                    lst.Add(sys7a00Dal.DelDetail(sys7a00Listid));
                    model = new Sys7a00();
                    model.Listid = sys7a00Listid;
                    //删除概要表
                    lst.Add(this.Delete(model));
                }
            }

            return CommonManager.Entity.SaveData(lst);
        }


        #endregion
    }
}




